<?php

namespace App\Services;

use App\Jobs\AudioBook;
use App\Models\TaskLog;

class AudioBookTaskService
{
    public function create($params)
    {
        // 记录任务日志
        if (empty($params) || empty($params['book_id']) || !isset($params['chapter_id']) || empty($params['type']) || empty($params['type_name'])) {
            return false;
        }

        $book_id = $params['book_id'];
        $chapter_id = $params['chapter_id'];
        $type = $params['type'];
        $type_name = $params['type_name'];

        $jobLog = TaskLog::updateOrCreate(
            ['book_id' => $book_id, 'chapter_id' => $chapter_id, 'type' => $type],
            ['type_name' => $type_name, 'status' => 0, 'start_time' => null, 'end_time' => null, 'params' => null, 'result' => null]
        );

        $params['log_id'] = $jobLog->id;

        AudioBook::dispatch($params);

        return true;
    }
}
